package org.jeecg.modules.lab.chemical.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.lab.chemical.entity.*;
import org.jeecg.modules.lab.chemical.service.ILabChemicalService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 标物(化学品)信息管理表
 * @Author: jeecg-boot
 * @Date: 2023-08-15
 * @Version: V1.0
 */
@Api(tags = "标物(化学品)信息管理表")
@RestController
@RequestMapping("/chemical/labChemical")
@Slf4j
public class LabChemicalController extends JeecgController<LabChemical, ILabChemicalService> {
    @Autowired
    private ILabChemicalService labChemicalService;

    @Autowired
    private ILabCronJobService labCronJobService;

    /**
     * 分页列表查询
     *
     * @param labChemical
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "标物(化学品)信息管理表-分页列表查询")
    @ApiOperation(value = "标物(化学品)信息管理表-分页列表查询", notes = "标物(化学品)信息管理表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<LabChemical>> queryPageList(LabChemical labChemical,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<LabChemical> queryWrapper = QueryGenerator.initQueryWrapper(labChemical, req.getParameterMap());
        Page<LabChemical> page = new Page<LabChemical>(pageNo, pageSize);
        IPage<LabChemical> pageList = labChemicalService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping(value = "/listBy")
    public Result<IPage<LabChemical>> queryList(LabChemical labChemical,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) {
        QueryWrapper<LabChemical> queryWrapper = QueryGenerator.initQueryWrapper(labChemical, req.getParameterMap());
        queryWrapper.ne("status", "已报废");
        Page<LabChemical> page = new Page<LabChemical>(pageNo, pageSize);
        IPage<LabChemical> pageList = labChemicalService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    //查询厂家
    @GetMapping(value = "/listFactory")
    public Result<List<ChemicalFactory>> queryFactoryList() {
        List<ChemicalFactory> chemicalFactories = labChemicalService.queryFactory();
        return Result.ok(chemicalFactories);
    }

    //查询类别
    @GetMapping(value = "/listType")
    public Result<List<ChemicalType>> queryTypeList() {
        List<ChemicalType> chemicalTypes = labChemicalService.queryType();
        return Result.ok(chemicalTypes);
    }

    //查询标物名称
    @GetMapping("/listName")
    public Result<List<ChemicalName>> queryNameList() {
        List<ChemicalName> chemicalNames = labChemicalService.queryName();
        return Result.ok(chemicalNames);
    }


    /**
     * 添加
     *
     * @param labChemical
     * @return
     */
    @AutoLog(value = "标物(化学品)信息管理表-添加")
    @ApiOperation(value = "标物(化学品)信息管理表-添加", notes = "标物(化学品)信息管理表-添加")
//	@RequiresPermissions("chemical:lab_chemical:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody LabChemical labChemical) {
        labChemical.setInventoryNow(labChemical.getNumber());
        labChemicalService.save(labChemical);

        if (MyEnum.sync) {
            long time = new Date().getTime();
            LabCronJob job = new LabCronJob("lab_chemical", "0", time, 1);
            labCronJobService.save(job);
        }
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param labChemical
     * @return
     */
    @AutoLog(value = "标物(化学品)信息管理表-编辑")
    @ApiOperation(value = "标物(化学品)信息管理表-编辑", notes = "标物(化学品)信息管理表-编辑")
//	@RequiresPermissions("chemical:lab_chemical:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody LabChemical labChemical) {
        labChemical.setInventoryNow(labChemical.getNumber());
        labChemical.setSynFlag("1");
        labChemicalService.updateById(labChemical);

        if (MyEnum.sync) {
            long time = new Date().getTime();
            LabCronJob job = new LabCronJob("lab_chemical", "1", time, 1);
            labCronJobService.save(job);
        }
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "标物(化学品)信息管理表-通过id删除")
    @ApiOperation(value = "标物(化学品)信息管理表-通过id删除", notes = "标物(化学品)信息管理表-通过id删除")
//	@RequiresPermissions("chemical:lab_chemical:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        labChemicalService.removeById(id);

        if (MyEnum.sync) {
            long time = new Date().getTime();
            String condition="id='"+id+"'";
            LabCronJob job = new LabCronJob("lab_chemical", "2",condition, time, 1);
            labCronJobService.save(job);
        }
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "标物(化学品)信息管理表-批量删除")
    @ApiOperation(value = "标物(化学品)信息管理表-批量删除", notes = "标物(化学品)信息管理表-批量删除")
//	@RequiresPermissions("chemical:lab_chemical:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.labChemicalService.removeByIds(Arrays.asList(ids.split(",")));

        if (MyEnum.sync) {
            long time = new Date().getTime();
            String[] split = ids.split(",");
            labCronJobService.saveCronBatch("lab_chemical","2",time,split);
        }
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "标物(化学品)信息管理表-通过id查询")
    @ApiOperation(value = "标物(化学品)信息管理表-通过id查询", notes = "标物(化学品)信息管理表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LabChemical> queryById(@RequestParam(name = "id", required = true) String id) {
        LabChemical labChemical = labChemicalService.getById(id);
        if (labChemical == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(labChemical);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param labChemical
     */
//    @RequiresPermissions("chemical:lab_chemical:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabChemical labChemical) {
        return super.exportXls(request, labChemical, LabChemical.class, "标物(化学品)信息管理表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequiresPermissions("chemical:lab_chemical:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabChemical.class);
    }

}
